const { checkSignature } = require('../utils/crypto');
const wechatConfig = require('../config/wechat');

// 验证微信服务器签名
const verifySignature = (req, res, next) => {
  const { signature, timestamp, nonce, echostr } = req.query;
  
  if (checkSignature(wechatConfig.token, signature, timestamp, nonce)) {
    if (req.method === 'GET') {
      res.send(echostr);
    } else {
      next();
    }
  } else {
    res.status(403).send('Invalid signature');
  }
};

module.exports = verifySignature;